package com.pan.sample.ui.start;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import com.blankj.utilcode.util.DeviceUtils;
import com.blankj.utilcode.util.NetworkUtils;
import com.pan.sample.BR;
import com.pan.sample.R;
import com.pan.sample.constants.FilePaths;
import com.pan.sample.databinding.ActivityStartBinding;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.zhongjh.mvvmrapid.base.ui.BaseActivity;
import com.zhongjh.mvvmrapid.utils.PermissionsUtil;
import com.zhongjh.mvvmrapid.utils.WindowUtil;

import java.io.File;

import io.reactivex.disposables.Disposable;

/**
 * 用于直接复制
 *
 * @author zhongjh
 * @date 2021/4/15
 */
public class StartActivity extends BaseActivity<ActivityStartBinding, StartViewModel> {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        WindowUtil.setFullScreen(StartActivity.this, false);
        super.onCreate(savedInstanceState);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_start;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public boolean isStartAutoHideSoftKeyboard() {
        return false;
    }

    @Override
    public void initParam() {
    }

    @Override
    public void initData() {
        viewModel.accept(requestMorePermissions());
    }

    @Override
    public void initViewObservable() {

    }

    /**
     * 请求多个权限
     */
    private Disposable requestMorePermissions() {
        String[] permissionsGroup = {
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.READ_PHONE_STATE
        };

        // 判断权限
        final RxPermissions rxPermissions = new RxPermissions(StartActivity.this);
        return rxPermissions
                .requestEachCombined(permissionsGroup)
                .subscribe(permission -> PermissionsUtil.managePermissions(permission, StartActivity.this, new PermissionsUtil.OnPermissions() {
                    @Override
                    public void onGranted() {
                        initView();
                    }
                }));
    }

    /**
     * 初始化数据
     */
    @SuppressLint("SetTextI18n")
    private void initView() {
        binding.txtIpAddress.setText(getResources().getString(R.string.iPAddressText) + ":" + NetworkUtils.getIPAddress(true));
        binding.txtMacAddress.setText(getResources().getString(R.string.macAddressText) + ":" + DeviceUtils.getMacAddress());
        binding.txtCode.setText(getResources().getString(R.string.codeText) + ":" + DeviceUtils.getUniqueDeviceId());
        binding.txtVersion.setText(getResources().getString(R.string.versionText) + ":" + DeviceUtils.getSDKVersionName());
    }

    /**
     * 初始化外部储存配置文件
     */
    private void initConfigFile() {
        File ConfigFile = FilePaths.getConfigDir(StartActivity.this);
        if (!ConfigFile.exists()) {
            ConfigFile.mkdir();
            copyResourceFile(com.pan.panframework.R.raw.ftpconfig, ConfigFile + "/ftpconfig.json");
        }

    }

}
